﻿using RuoVea.EventBus;
using RuoVea.ExLog;

namespace RuoVea.Tool
{
    /// <summary>
    /// 
    /// </summary>
    public class LogEventSubscriber : IEventSubscriber
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        public LogEventSubscriber(IServiceProvider services)
        {
            Services = services;
        }

        /// <summary>
        /// 
        /// </summary>
        public IServiceProvider Services { get; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        [EventSubscribe("Create:OpLog")]
        public async Task CreateOpLog(EventHandlerExecutingContext context)
        {
            LogFactory.Info(context.Source.Payload+"");
            //using var scope = Services.CreateScope();
            //var _repository = scope.ServiceProvider.GetRequiredService<ISqlSugarRepository<SysLogOp>>();
            //var log = (SysLogOp)context.Source.Payload;
            //await _repository.InsertAsync(log);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        [EventSubscribe("Create:ExLog")]
        public async Task CreateExLog(EventHandlerExecutingContext context)
        {
            LogFactory.Error(context.Source.Payload+"");
            //using var scope = Services.CreateScope();
            //var _repository = scope.ServiceProvider.GetRequiredService<ISqlSugarRepository<SysLogEx>>();
            //var log = (SysLogEx)context.Source.Payload;
            //await _repository.InsertAsync(log);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        [EventSubscribe("Create:VisLog")]
        public async Task CreateVisLog(EventHandlerExecutingContext context)
        {
            LogFactory.Info(context.Source.Payload+"");
            //using var scope = Services.CreateScope();
            //var _repository = scope.ServiceProvider.GetRequiredService<ISqlSugarRepository<SysLogVis>>();
            //var log = (SysLogVis)context.Source.Payload;
            //await _repository.InsertAsync(log);
        }

        /// <summary>
        /// 打印sql
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        [EventSubscribe("LogEvent:Info")]
        public async Task CreateSqlInfo(EventHandlerExecutingContext context)
        {
            LogFactory.Info(context.Source.Payload+"");
        }
        

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="context"></param>
        ///// <returns></returns>
        //[EventSubscribe("Update:UserLoginInfo")]
        //public async Task UpdateUserLoginInfo(EventHandlerExecutingContext context)
        //{
        //    using var scope = Services.CreateScope();
        //    var _repository = scope.ServiceProvider.GetRequiredService<ISqlSugarRepository<SysUser>>();
        //    var log = (SysUser)context.Source.Payload;
        //    await _repository.Context.Updateable(log).UpdateColumns(m => new { m.LastLoginTime, m.LastLoginIp })
        //        .ExecuteCommandAsync();
        //}
    }
}
